package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.network.socket.response.GetNewAppResult;

import common.utils.DensityUtil;

/**
 * 确认升级对话框
 * @author yzq
 *
 */
public class MyUpdateCommitDialog extends Dialog {
	private UpdateCommitDialogCallBack callBack;
	private GetNewAppResult appInfo;

	public MyUpdateCommitDialog(Context context, GetNewAppResult appInfo, UpdateCommitDialogCallBack callBack) {
		super(context);
		this.callBack = callBack;
		this.appInfo = appInfo;
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dialog_update_app);

		setDialogAttributes();
		initDialogView();
	}

	public void setDialogAttributes() {
		Window dialogWindow = getWindow();
		setCanceledOnTouchOutside(false);
		dialogWindow.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		
		DisplayMetrics dm = new DisplayMetrics();
		dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
		// lp.alpha = 0.9f; // 透明度

		dialogWindow.setAttributes(lp);
		dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	}

	public void initDialogView() {
		//版本号
		TextView app_version_tv = (TextView) findViewById(R.id.app_version_tv);
		app_version_tv.setText(getContext().getString(R.string.version_prefix)+appInfo.result.ver);
		//文件大小
		TextView app_size_tv = (TextView) findViewById(R.id.app_size_tv);
		app_size_tv.setText(getContext().getString(R.string.package_size)+Formatter.formatFileSize(getContext(), Long.parseLong(appInfo.result.size)));
		//发布日期
		TextView app_release_time_tv = (TextView) findViewById(R.id.app_release_time_tv);
		app_release_time_tv.setText(getContext().getString(R.string.release_date)+appInfo.result.time);
		//更新描述
		TextView dialog_msg_tv = (TextView) findViewById(R.id.dialog_msg_tv);
		dialog_msg_tv.setText(Html.fromHtml(appInfo.result.content));
		
//		CheckBox ignore_cb = (CheckBox) findViewById(R.id.ignore_cb);
//		ignore_cb.setOnCheckedChangeListener(callBack);
		
		findViewById(R.id.sureBtn).setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View arg0, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					callBack.onCommit();
					dismiss();
				}
				return false;
			}
		});

//		appInfo.result.primary = "1";
		if ("1".equals(appInfo.result.primary)){
			findViewById(R.id.cancelBtn).setEnabled(false);
			setCancelable(false);
		}else{
			findViewById(R.id.cancelBtn).setOnTouchListener(new OnTouchListener() {
				public boolean onTouch(View arg0, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_UP) {
						callBack.onCancel();
						dismiss();
					}
					return false;
				}
			});
		}
	}

	public interface UpdateCommitDialogCallBack{
		/**
		 * 点击确定按钮时回调
		 */
		void onCommit();
		/**
		 * 取消时回调
		 */
		void onCancel();
	}
}
